package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.search.repository.AlbumRepository;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.search.service.MqOpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Optional;

/**
 * @Author：yyj
 * @Package：com.atguigu.tingshu.search.service.impl
 * @Project：tingshu_parent240829
 * @name：MqOpsServiceImpl
 * @Date：2025/1/20 18:39
 * @Filename：MqOpsServiceImpl
 */
@Service
public class MqOpsServiceImpl implements MqOpsService {
    @Autowired
    private ItemService itemService;
    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private AlbumRepository albumRepository;
    @Override
    public void listenOnSaleAlbum(String content) {
        itemService.onAlbumInfos(Long.parseLong(content));
    }

    @Override
    public void listenOffSaleAlbum(String content) {
        itemService.offAlbumEs(Long.parseLong(content));
    }

    @Override
    public void updateAlbumStat(String orderNo) {
        //根据订单编号查询订单信息
        Result<OrderInfo> orderInfoResult = orderInfoFeignClient.getOrderInfoByOrderNo(orderNo);
        OrderInfo orderInfoData = orderInfoResult.getData();
        Assert.notNull(orderInfoData, "远程查询订单信息失败");
        // 2.订单的类型
        String itemType = orderInfoData.getItemType();
        Long albumId = null;
        // 买的是专辑（修改播放量）
        if("1001".equals(itemType)) {
            albumId = orderInfoData.getOrderDetailList().get(0).getItemId();
            Optional<AlbumInfoIndex> albumInfoIndex = albumRepository.findById(albumId);
            albumInfoIndex.ifPresent(oldAlbumInfoIndex -> {
                Integer buyStatNum = oldAlbumInfoIndex.getBuyStatNum();
                oldAlbumInfoIndex.setBuyStatNum(buyStatNum + orderInfoData.getOrderDetailList().size());
                albumRepository.save(oldAlbumInfoIndex);
            });
        }// 买的是声音（修改专辑播放量）
        else if ("1002".equals(itemType)) {
            Long trackId  = orderInfoData.getOrderDetailList().get(0).getItemId();
            Result<TrackInfo> trackInfoById = albumInfoFeignClient.getTrackInfoById(trackId);
            TrackInfo trackInfoData = trackInfoById.getData();
            Assert.notNull(trackInfoData, "远程查询声音信息失败");
            albumId = trackInfoData.getAlbumId();
            Optional<AlbumInfoIndex> albumInfoIndex  = albumRepository.findById(albumId);
            albumInfoIndex.ifPresent(oldAlbumInfoIndex ->{
                Integer buyStatNum = oldAlbumInfoIndex.getBuyStatNum();
                oldAlbumInfoIndex.setBuyStatNum(buyStatNum+orderInfoData.getOrderDetailList().size());
                albumRepository.save(oldAlbumInfoIndex);
            });
        } else {
            // 买的是vip
        }
    }
}